package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;

/*
 * LoanUI - MAY 2014 - UCN - dmaa0214 - Group 4
 */

public class LoanUI
{
    private LoanCtr loanCtr;

    public LoanUI()
    {
        loanCtr = new LoanCtr();
    }
    
    public void start()
    {
        LoanMenu();
    }

    public void LoanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise    
           int choice = writeLoanMenu();
            if (choice == 1)
            {
                findLoan();
                waitInput();
            }
            else{if(choice == 2){
                   createLoan();
                   waitInput();
                }
                else{
                    if(choice == 3){
                        deleteLoan();
                        waitInput();
                    }
                    else{
                        if(choice == 4){
                             listAllLoans();
                             waitInput();
                        }
                        else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
        }//end while
    }
    
    private int writeLoanMenu()
    {       
        // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Loan Menu ***");
            System.out.println(" (1) Show information about a loan");
            System.out.println(" (2) Create loan");
            System.out.println(" (3) Delete loan");
            System.out.println(" (4) List all loans");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    private void waitInput()
    {
        System.out.println("Press enter to continue");
        new Scanner(System.in).nextLine();
    }
    
    public long inputID()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write ID: ");
        long id = keyboard.nextLong();
        return id;
    }
    
    public long inputSearch()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write search ID: ");
        long search = keyboard.nextLong();
        return search;
    }
    
    public int inputBorrowDay()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write the day of the month: ");
        int day = keyboard.nextInt();
        return day;
    }
    
    public int inputBorrowMonth()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println(" Write the month: ");
       int month = keyboard.nextInt();
       return month;
    }
    
    public int inputReturnDay()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write the day that the dvd should be returned: ");
        int day = keyboard.nextInt();
        return day;
    }
    
    public int inputReturnMonth()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write the month the dvd should be returned: ");
        int month = keyboard.nextInt();
        return month;
    }
    
    public String inputPersonNameSearch()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write the name of the person that wants to borrow the dvd: ");
        String nameSearch = keyboard.nextLine();
        return nameSearch;
    }
    
    public String inputDvdCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write the name of the DVD: ");
        String dvdCopy = keyboard.nextLine();
        return dvdCopy;
    }
    
    private void createLoan()
    {
        String personName = inputPersonNameSearch();
        if (AddressBook.getInstance().getPerson(personName) != null)
        {   
            Person p = AddressBook.getInstance().getPerson(personName);
            String dvdTitle = inputDvdCopy();
            
            if (DVDContainer.getInstance().findDVD(dvdTitle) != false)
            {
                int borrowDay = inputBorrowDay();
                int borrowMonth = inputBorrowMonth();
                int returnDay = inputReturnDay();
                int returnMonth = inputReturnMonth();
        
                if (personName != null && dvdTitle != null && borrowDay > 0 && borrowMonth > 0 &&
                    returnDay > 0 && returnMonth > 0)
                {   
                    Copy dvdCopy = DVDContainer.getInstance().findCopy(dvdTitle);
                    loanCtr.createLoan(p, dvdTitle, borrowDay, borrowMonth, returnDay, returnMonth);
                }
                else
                {
                    System.out.println("You need to input the parameters correct!");
                }
            }
            else
            {
                System.out.println("DVD " + dvdTitle + " is not listed!");
            }
        }
        else
        {
            System.out.println("Person " + personName + " is not listed!");
        }
            
          
    }  

    private void findLoan()
    {
        long search = inputID();
        Loan foundLoan = LoanContainer.getInstance().findLoan(search);
        System.out.println(foundLoan.getLoanPerson() + ". ");
        System.out.println(foundLoan.getDVDTitle() + ". ");        
    }    
    
    private void deleteLoan()
    {
        long searchID = inputID();
        loanCtr.deleteLoan(searchID);
    }
    
    private void listAllLoans()
    {
        loanCtr.listAllLoans();
    }           
}








